<template>
  <div class="page-list">
    <div class="page-header">
      <el-button type="primary" @click="handleCreate">
        <el-icon><Plus /></el-icon>
        新建页面
      </el-button>
    </div>

    <el-table :data="pageList" border stripe>
      <el-table-column prop="title" label="页面标题" />
      <el-table-column prop="path" label="访问路径" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button-group>
            <el-button type="primary" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button type="info" @click="handlePreview(row)">
              <el-icon><View /></el-icon>
              预览
            </el-button>
            <el-button type="danger" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Plus, Edit, View, Delete } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()

const pageList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取页面列表
const getPageList = async () => {
  try {
    // TODO: 调用接口获取页面列表
    const response = await fetch('/api/pages', {
      params: {
        page: currentPage.value,
        size: pageSize.value
      }
    })
    const data = await response.json()
    pageList.value = data.list
    total.value = data.total
  } catch (error) {
    console.error('获取页面列表失败:', error)
  }
}

// 新建页面
const handleCreate = () => {
  router.push({
    name: 'PageEditor',
    params: { id: 'new' }
  })
}

// 编辑页面
const handleEdit = (row) => {
  router.push({
    name: 'PageEditor',
    params: { id: row.id }
  })
}

// 预览页面
const handlePreview = (row) => {
  router.push({
    name: 'PagePreview',
    params: { id: row.id }
  })
}

// 删除页面
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该页面吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      // TODO: 调用接口删除页面
      await fetch(`/api/pages/${row.id}`, {
        method: 'DELETE'
      })
      getPageList()
    } catch (error) {
      console.error('删除页面失败:', error)
    }
  })
}

// 分页大小变化
const handleSizeChange = (val) => {
  pageSize.value = val
  getPageList()
}

// 当前页变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  getPageList()
}

onMounted(() => {
  getPageList()
})
</script>

<style scoped>
.page-list {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}
</style> 